package com.zhang.family_doctor.service.impl;

import com.zhang.family_doctor.dto.PackagesListDto;
import com.zhang.family_doctor.dto.PackagesDto;
import com.zhang.family_doctor.entity.*;
import com.zhang.family_doctor.mapper.*;
import com.zhang.family_doctor.service.PackagesService;
import com.zhang.family_doctor.utils.CommonUtils;
import com.zhang.family_doctor.utils.TokenUtils;
import com.zhang.family_doctor.vo.PackagesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PackagesServiceImpl implements PackagesService {

    @Autowired
    private PackagesMapper packagesMapper;

    @Autowired
    private PackagesTagMapper packagesTagMapper;

    @Autowired
    private ServicesPackagesMapper servicesPackagesMapper;

    @Autowired
    private PackageDetailsMapper packageDetailsMapper;

    @Autowired
    private EditRecordMapper editRecordMapper;

    /**
     * 新增服务包
     *
     * @param packagesDto
     * @param request
     * @return
     */
    @Override
    @Transactional
    public R<String> save(PackagesDto packagesDto, HttpServletRequest request) {
        //从token中拿到到登录名
        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);
        //给服务包生成一个id
        int id = CommonUtils.genUUID();
        packagesDto.setId(id);
        packagesDto.setCreateTime(new Date());
        packagesDto.setUpdateTime(new Date());
        packagesDto.setCreated(getName);
        //创建服务包默认审核状态
        packagesDto.setState(1);
        packagesMapper.savePackages(packagesDto);

        //向服务包标签表批量插入数据
        List<PackagesTag> packagesTagList = packagesDto.getPackagesTagList();
        for (PackagesTag packagesTag : packagesTagList) {
            packagesTag.setId(CommonUtils.genUUID());
            packagesTag.setPackagesId(id);
        }
        packagesTagMapper.savePackagesTag(packagesTagList);

        //向服务包项目表批量插入数据
        List<ServicesPackages> servicesPackagesList = packagesDto.getServicesPackagesList();
        for (ServicesPackages servicesPackages : servicesPackagesList) {
            servicesPackages.setId(CommonUtils.genUUID());
            servicesPackages.setPackagesId(id);
        }
        servicesPackagesMapper.saveServicesPackages(servicesPackagesList);

        //向服务包详情表批量插入数据
        List<PackageDetails> packageDetailsList = packagesDto.getPackageDetailsList();
        for (PackageDetails packageDetails : packageDetailsList) {
            packageDetails.setId(CommonUtils.genUUID());
            packageDetails.setPackagesId(id);
        }
        packageDetailsMapper.savePackageDetails(packageDetailsList);

        //向编辑记录表插入记录
        EditRecord editRecord = new EditRecord();

        editRecord.setId(CommonUtils.genUUID());
        editRecord.setPackageId(id);
        editRecord.setCreateTime(new Date());
        editRecord.setSpecificSteps(1);
        editRecord.setOperator(getName);

        editRecordMapper.save(editRecord);

        return R.success("插入成功！");
    }

    /**
     * 审核服务包
     *
     * @param id      服务包id
     * @param request
     * @return
     */
    @Override
    @Transactional
    public R<String> process(Integer id, HttpServletRequest request) {
        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        Packages packages = new Packages();
        packages.setReviewed(getName);
        packages.setId(id);
        //修改为使用中状态
        packages.setState(2);
        packages.setProcessTime(new Date());

        packagesMapper.process(packages);

        //生成审核通过的日志
        EditRecord editRecord = new EditRecord();
        editRecord.setId(CommonUtils.genUUID());
        editRecord.setReviewer(getName);
        editRecord.setCreateTime(new Date());
        editRecord.setSpecificSteps(3);
        editRecord.setPackageId(id);

        editRecordMapper.process(editRecord);

        return R.success("审核通过！");
    }

    /**
     * 服务包驳回
     *
     * @param id
     * @param rejectionContent 驳回说明
     * @param request
     * @return
     */
    @Override
    @Transactional
    public R<String> reject(Integer id, String rejectionContent, HttpServletRequest request) {
        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        Packages packages = new Packages();
        packages.setId(id);
        packages.setRejectionContent(rejectionContent);
        packages.setReviewed(getName);
        //状态改为已驳回
        packages.setState(3);
        packages.setProcessTime(new Date());

        packagesMapper.reject(packages);

        EditRecord editRecord = new EditRecord();
        editRecord.setId(CommonUtils.genUUID());
        editRecord.setPackageId(id);
        editRecord.setSpecificSteps(4);
        editRecord.setReviewer(getName);
        editRecord.setCreateTime(new Date());
        editRecord.setDismissal(rejectionContent);

        editRecordMapper.reject(editRecord);

        return R.success("驳回成功！");
    }

    /**
     * 服务包停用
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    @Transactional
    public R<String> disable(Integer id, HttpServletRequest request) {
        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        Packages packages = new Packages();
        packages.setId(id);
        packages.setReviewed(getName);
        //改成已停用状态
        packages.setState(4);
        packages.setUpdateTime(new Date());

        packagesMapper.update(packages);

        EditRecord editRecord = new EditRecord();
        editRecord.setId(CommonUtils.genUUID());
        editRecord.setPackageId(id);
        editRecord.setOperator(getName);
        editRecord.setCreateTime(new Date());
        editRecord.setSpecificSteps(6);

        editRecordMapper.disableOrEnable(editRecord);

        return R.success("停用成功！");
    }

    /**
     * 服务包启用
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    @Transactional
    public R<String> enable(Integer id, HttpServletRequest request) {
        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        Packages packages = new Packages();
        packages.setId(id);
        packages.setReviewed(getName);
        //改成已使用状态
        packages.setState(2);
        packages.setUpdateTime(new Date());

        packagesMapper.update(packages);

        EditRecord editRecord = new EditRecord();
        editRecord.setId(CommonUtils.genUUID());
        editRecord.setPackageId(id);
        editRecord.setOperator(getName);
        editRecord.setCreateTime(new Date());
        editRecord.setSpecificSteps(5);

        editRecordMapper.disableOrEnable(editRecord);

        return R.success("启用成功！");
    }

    /**
     * 编辑服务包
     *
     * @param packagesDto
     * @param request
     * @return
     */
    @Override
    @Transactional
    public R<String> updatePackage(PackagesDto packagesDto, HttpServletRequest request) {

        //从token中拿到到登录名
        String tokenBackstage = request.getHeader("tokenBackstage");
        String getName = TokenUtils.verifyAndGetName(tokenBackstage);

        Integer id = packagesDto.getId();

        List<PackagesTag> packagesTagList = packagesDto.getPackagesTagList();
        List<ServicesPackages> servicesPackagesList = packagesDto.getServicesPackagesList();
        List<PackageDetails> packageDetailsList = packagesDto.getPackageDetailsList();

        packagesDto.setModified(getName);
        packagesDto.setUpdateTime(new Date());
        //修改之后提交重新审核
        packagesDto.setState(1);

        packagesMapper.updatePackage(packagesDto);

        //根据服务包id删除所有标签，再进行插入
        packagesTagMapper.delete(id);
        for (PackagesTag packagesTag : packagesTagList) {
            packagesTag.setId(CommonUtils.genUUID());
            packagesTag.setPackagesId(id);
        }
        packagesTagMapper.savePackagesTag(packagesTagList);

        //根据服务包id删除所有的服务详情
        packageDetailsMapper.delete(id);
        for (PackageDetails packageDetails : packageDetailsList) {
            packageDetails.setId(CommonUtils.genUUID());
            packageDetails.setPackagesId(id);
        }
        packageDetailsMapper.savePackageDetails(packageDetailsList);

        //根据服务包id删除所有的服务项目
        servicesPackagesMapper.delete(id);
        for (ServicesPackages servicesPackages : servicesPackagesList) {
            servicesPackages.setId(CommonUtils.genUUID());
            servicesPackages.setPackagesId(id);
        }
        servicesPackagesMapper.saveServicesPackages(servicesPackagesList);

        return R.success("编辑成功！");
    }

    /**
     * 根据服务包类型查询
     *
     * @param packagesListDto
     * @return
     */
    @Override
    public R<PackagesListDto> getAll(PackagesListDto packagesListDto) {

        List<Packages> all = packagesMapper.getAll(packagesListDto);

        List<Packages> packagesListOne = new ArrayList<>();
        List<Packages> packagesListTwo = new ArrayList<>();
        List<Packages> packagesListThree = new ArrayList<>();
        List<Packages> packagesListFour = new ArrayList<>();
        for (Packages packages : all) {
            if (packages.getPackageType() == 1) {
                packagesListOne.add(packages);
            } else if (packages.getPackageType() == 2) {
                packagesListTwo.add(packages);
            } else if (packages.getPackageType() == 3) {
                packagesListThree.add(packages);
            } else if (packages.getPackageType() == 4) {
                packagesListFour.add(packages);
            }
        }
        packagesListDto.setPackagesListOne(packagesListOne);
        packagesListDto.setPackagesListTwo(packagesListTwo);
        packagesListDto.setPackagesListThree(packagesListThree);
        packagesListDto.setPackagesListFour(packagesListFour);

        return R.success(packagesListDto);

    }

    /**
     * 查看服务包详情
     *
     * @param id
     * @return
     */
    @Override
    public R<PackagesVO> getPackageDetails(Integer id) {
        PackagesVO packagesVO = new PackagesVO();
        //查主表关联详情表
        Packages selectPackagesById = packagesMapper.selectPackagesById(id);

        //根据服务包id 查出标签
        List<Tag> tagList = packagesTagMapper.selectTagByPackagesId(id);

        //根据服务包id 查出服务项目
        List<Services> servicesList = servicesPackagesMapper.selectServicesByPackagesId(id);

        //根据服务包id 查出服务详情
        List<PackageDetails> packageDetailsList = packageDetailsMapper.selectDetailsByPackagesId(id);

        //根据服务包id 查出编辑记录日志表
        List<EditRecord> editRecordList = editRecordMapper.selectEditRecordByPackagesId(id);

        packagesVO.setPackages(selectPackagesById);
        packagesVO.setTagList(tagList);
        packagesVO.setServicesList(servicesList);
        packagesVO.setPackageDetailsList(packageDetailsList);
        packagesVO.setEditRecordList(editRecordList);

        return R.success(packagesVO);
    }
}
